import sys
from matplotlib import pyplot as plt
from Ui_Incaui import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow,QFileDialog,QApplication
from PyQt5.QtGui import QColor
 
 
class MyMainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self,parent =None):
        super(MyMainWindow,self).__init__(parent)
        self.setupUi(self)
    
    # 打开文件
    def openfile(self):
        # 清空列表
        l=[]
        self.textBrowser.clear()
        self.textEdit.clear()
        self.textEdit_2.clear()
        s=''
        f=''
        if len(plt.get_fignums())!=0:
            plt.close()
        # 对话框用于选择文件或目录
        options = QFileDialog.Options()
        mdf_file_path, _ = QFileDialog.getOpenFileName(self, "打开文件", "", "Dat Files (*.dat);;All Files (*)", options=options)
        if mdf_file_path:
            import inca
            l,n=inca.inca(mdf_file_path,s,f)
            for i in range(len(l)):
                if i==0 or i==1 or i==n:
                    self.textEdit_3.setTextColor(QColor(255,0,0))
                    self.textEdit_3.append(l[i])
                    self.textEdit_3.append('\n')
                if i>1 and i!=n:                    
                    self.textEdit_3.setTextColor(QColor(0,0,0))
                    self.textEdit_3.append(l[i])
                    self.textEdit_3.append('\n')
        # 显示文件及其路径
        self.textBrowser.setText(mdf_file_path)
    
    # 查找数据
    def locating(self):
        l=[]
        # 获取文本内容
        mdf_file_path=self.textBrowser.toPlainText()
        # 获取当前选项文本
        s=self.textEdit.toPlainText()
        f=self.textEdit_2.toPlainText()
        if len(plt.get_fignums())!=0:
            plt.close()
        if mdf_file_path:
            import inca
            if (s != '') and f:
                    l,n=inca.inca(mdf_file_path,float(s),float(f))
            else:
                l,n=inca.inca(mdf_file_path,s,f)
            for i in range(len(l)):
                if i==0 or i==1 or i==n:
                    self.textEdit_3.setTextColor(QColor(255,0,0))
                    self.textEdit_3.append(l[i])
                    self.textEdit_3.append('\n')
                if i>1 and i!=n:
                    self.textEdit_3.setTextColor(QColor(0,0,0))
                    self.textEdit_3.append(l[i])
                    self.textEdit_3.append('\n')
            self.textEdit_3.append('\n')
    
    # 关闭
    def close(self):
        # 退出
        quit()
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = MyMainWindow()
    myWin.show()    # 进入主体框口，没有这个打不开窗口
    sys.exit(app.exec_())    # 进入程序的主循环直到exit()被调用（只使用app.exec_()，程序一样可以正常运行，但是关闭窗口后进程却不会退出）